//
//  NTESLiveUtil.m
//  Netease
//
//  Created by 王方伟 on 2017/8/25.
//  Copyright © 2017年 wfw. All rights reserved.
//

#import "NTESLiveUtil.h"

@implementation NTESLiveUtil

+ (NSDictionary *)dictByJsonData:(NSData *)data{
    NSDictionary *dict = nil;
    if ([data isKindOfClass:[NSData class]]){
        NSError *error = nil;
        dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
        if (error) {
            SHLog(@"dictByJsonData failed %@ error %@",data,error);
        }
    }
    return [dict isKindOfClass:[NSDictionary class]] ? dict : nil;
}

+ (NSDictionary *)dictByJsonString:(NSString *)jsonString{
    if (!jsonString.length) {
        return nil;
    }
    NSData *data = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
    return [NTESLiveUtil dictByJsonData:data];
}

+ (NSString *)jsonString:(NSString *)destinationJsonString addJsonString:(NSString *)jsonString{
    if (!jsonString.length) {
        return destinationJsonString;
    }
    NSDictionary *sourceDict = [self dictByJsonString:jsonString];
    NSDictionary *destinationDict = [self dictByJsonString:destinationJsonString];
    NSMutableDictionary * destinationMutableDict = [destinationDict mutableCopy];
    [destinationMutableDict addEntriesFromDictionary:sourceDict];
    destinationJsonString = [self dataTojsonString:destinationMutableDict];
    return destinationJsonString;
}

+ (NSString *)dataTojsonString:(id)object{
    NSString *jsonString = nil;
    NSError *error;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:object options:NSJSONWritingPrettyPrinted error:&error];
    if (!jsonData) {
        SHLog(@"Got an error: %@", error);
    } else {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }
    return jsonString;
}

+ (NTESLiveType)stringToLiveType:(NSString *)string{
    if ([string isEqualToString:@"AUDIO"]) {
        return NTESLiveTypeAudio;
    }
    if ([string isEqualToString:@"VIDEO"]) {
        return NTESLiveTypeVideo;
    }
    return NTESLiveTypeInvalid;
}


+ (NSString *)liveTypeToString:(NTESLiveType)type{
    switch (type) {
        case NTESLiveTypeAudio:
            return @"AUDIO";
        case NTESLiveTypeVideo:
            return @"VIDEO";
        default:
            return @"";
    }
}

@end
